/*
 * AllowUserResource.cpp
 *
 *  Created on: 2013. 12. 27.
 *      Author: the2
 */

#include "AllowUserResource.h"
#include <Database/Static.h>
#include "../macro.h"

namespace ProjectZ { namespace NResource {

AllowUserResource::AllowUserResource()
{
}

AllowUserResource::~AllowUserResource()
{
}

bool
AllowUserResource::LoadResource()
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery(" SELECT NATIONAL_CODE, socialid FROM resource_allow_user");

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	_allowUserVector.clear();
	while( clsAuto->Next() )
	{
		Toolkit::Str<3> NATIONAL_CODE;
		Toolkit::Str<18> socialid;

		clsAuto->GetRecord() >> NATIONAL_CODE >> socialid;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		_allowUserVector.push_back(socialid.c_str());
	}

	return true;
}

bool
AllowUserResource::IsAllowUser(std::string socialid)
{
	auto iter = _allowUserVector.begin();
	for(; iter < _allowUserVector.end(); ++iter)
	{
		if( 0 == (*iter).compare(socialid) )
			return true;
	}

	return false;
}

} /* namespace NResource */ } /* namespace ProjectZ */
